package com.app.qtjk.lsh.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.app.common.utils.DateFormatUtil;
import com.app.common.utils.FileUploadUtiles;
import com.app.common.utils.FilterUtil;
import com.app.common.utils.ParamConfig;
import com.app.common.utils.ReqJsonUtil;
import com.app.common.utils.StringUtil;
import com.css.sword.kernel.base.annotation.Service;
import com.css.sword.kernel.base.annotation.ServiceContainer;
import com.css.sword.kernel.base.persistence.IPersistenceService;
import com.css.sword.kernel.base.persistence.SQLParam;
import com.css.sword.kernel.utils.SwordLogUtils;
import com.css.sword.kernel.utils.SwordPersistenceUtils;
import com.css.sword.platform.web.event.IReqData;
import com.css.sword.platform.web.event.IResData;
import com.css.sword.platform.web.event.SwordRes;

@ServiceContainer
public class RecruitService {

	private static SwordLogUtils log = SwordLogUtils.getLogger(RecruitService.class);

	public RecruitService() throws Exception {
		super();
	}


	/**
	 * 得到招聘列表信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Service(serviceName = "RecruitService_getList")
	public IResData getList(IReqData req) throws Exception {

		log.debug("开始进入 RecruitService_getList 方法。");
		IPersistenceService dao = SwordPersistenceUtils.getPersistenceService();
		HashMap commonMap = new HashMap();
		ArrayList commonList = new ArrayList();		
		HashMap hashMap = new HashMap();
		IResData res = new SwordRes();
		try {
			if (FilterUtil.checkToken(req) == false) {
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "请求口令错误！");
			}else {
					hashMap.put("returnStatus", "true");
					int pageNum=0;
					int pageSize=0;
					String pageSizeParamVal=ParamConfig.getInstanse().getParam("pageSize");
					SQLParam sqlParam = new SQLParam();
					StringBuffer macroparam = new StringBuffer();
					// 获取前台post的数据
					//String userId = ReqJsonUtil.getPostParam(req,"userId");
					//String sortType = ReqJsonUtil.getPostParam(req,"sortType");
					String pageNumStr=ReqJsonUtil.getPostParam(req,"pageNum");
					int totalCount = Integer.parseInt(dao.queryColumnByKey("RecruitService_getBrandListByCategoryId_count", sqlParam, 1).toString());
					if (pageNumStr.equals("")) {
						pageNum=1;
						pageSize=totalCount;
					}else {
						pageNum = Integer.parseInt(pageNumStr);
						pageSize=Integer.parseInt(pageSizeParamVal);
					}
					
					int totalPage=(totalCount%pageSize==0)?(totalCount/pageSize):((totalCount/pageSize)+1);
					int pageNumHt=(pageNum-1)*pageSize;
					
					macroparam.append(" limit "+pageNumHt+","+pageSize);
					/*if (sortType.equals("0")) {
						macroparam.append(" limit "+pageNumHt+","+pageSize);
					} else if (sortType.equals("1")) {
						macroparam.append(",c_brand_wealth DESC limit "+pageNumHt+","+pageSize);
					} else if (sortType.equals("2")) {
						macroparam.append(",c_brand_wealth ASC limit "+pageNumHt+","+pageSize);
					} else if (sortType.equals("3")) {
						macroparam.append(",c_brand_opptime DESC limit "+pageNumHt+","+pageSize);
					} else if (sortType.equals("4")) {
						macroparam.append(",c_brand_opptime ASC limit "+pageNumHt+","+pageSize);
					}*/
					sqlParam.addParam("shtg");
					sqlParam.addMacroParam(macroparam.toString());
	
					List<Map<String,Object>> recruitMapList = dao.queryMapListByKey("RecruitService_getBrandListByCategoryId", sqlParam);
					for (int i = 0; i < recruitMapList.size(); i++) {
						Map dataMap=recruitMapList.get(i);
						Set<String> keySet = dataMap.keySet();
						for (String s : keySet) {
							if (s.equals("zpLogo")) {
								String webfullpath = StringUtil.parse2String(FileUploadUtiles.getFileElemenstMap(StringUtil.parse2String(dataMap.get(s))).get("WebFullPath"));
								dataMap.put("zpLogo", webfullpath);
							}
							if (s.equals("webpath")) {
								dataMap.put("webpath", ParamConfig.getInstanse().getParam("webDomainPath")+StringUtil.parse2String(dataMap.get(s)));
							}
							if (s.equals("zpShsj")) {
								dataMap.put("zpShsj", DateFormatUtil.date2str((Date)dataMap.get(s),DateFormatUtil.DATE_FORMAT));
							}
						}
					}
					hashMap.put("totalPage", totalPage);
					hashMap.put("pageNum", pageNum);
					hashMap.put("returnStatus", "true");
					hashMap.put("msg", "成功返回，未出现异常！");
					commonMap.put("recruitMapList", recruitMapList);
				}	
			} catch (Exception e) {
				log.debug("---------------系统发生异常！---------------");
				e.printStackTrace();
				hashMap.put("returnStatus", "false");
				hashMap.put("msg", "出现异常！");
			}finally{
				commonMap.put("commonData", hashMap);
				commonList.add(commonMap);
				res.addListWithDataName("returnData", commonList);
			}
			return res;
	}

}